﻿using System;
using UnityEngine;

namespace Core
{

    // ReSharper disable InvalidXmlDocComment
    /// <summary>
    /// 若要使用该类型作为Dict的key，则需要实现IComparable, IComparable<LWH>,接口
    /// </summary>
    // ReSharper disable once InconsistentNaming
    public struct LWH : IEquatable<LWH>
    {
        public static readonly LWH Invalid = Parse(float.Epsilon, float.Epsilon, float.Epsilon);

        public float Length;
        public float Width;
        public float Height;

        public LWH(float l, float w, float h)
        {
            Length = l;
            Width = w;
            Height = h;
        }

        public static LWH Parse(float l, float w, float h)
        {
            LWH point;
            point.Length = l;
            point.Width = w;
            point.Height = h;
            return point;
        }

        public static LWH operator +(LWH point, LWH vec)
        {
            LWH newPoint;
            newPoint.Length = point.Length + vec.Length;
            newPoint.Width = point.Width + vec.Width;
            newPoint.Height = point.Height + vec.Height;
            return newPoint;
        }

        public static LWH operator *(LWH point, float value)
        {
            LWH newPoint;
            newPoint.Length = point.Length * value;
            newPoint.Width = point.Width * value;
            newPoint.Height = point.Height * value;
            return newPoint;
        }

        public static LWH operator -(LWH point, LWH vec)
        {
            LWH newPoint;
            newPoint.Length = point.Length - vec.Length;
            newPoint.Width = point.Width - vec.Width;
            newPoint.Height = point.Height - vec.Height;
            return newPoint;
        }

        public bool Equals(LWH other)
        {
            return Mathf.Approximately(Length, other.Length) &&
                Mathf.Approximately(Width, other.Width) &&
                Mathf.Approximately(Height, other.Height);
        }

        public override bool Equals(object obj)
        {
            if (obj == null || (obj is LWH) == false)
            {
                return false;
            }
            return Equals((LWH)obj);
        }

        public override int GetHashCode()
        {
            return Length.GetHashCode() ^ (Width.GetHashCode() << 2) ^ (Height.GetHashCode() >> 2);
        }

        public override string ToString()
        {
            return $"({Width},{Height})";
        }
    }
}

